cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

file(STRINGS VERSION version)

list(GET version 0 PROJECT_VERSION_MAJOR)
list(GET version 1 PROJECT_VERSION_MINOR)
list(GET version 2 PROJECT_VERSION_PATCH)

SET(PROJECT_VERSION_STRING "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
add_compile_definitions(PROJECT_VERSION_STRING="${PROJECT_VERSION_STRING}")

project(ImStudio C CXX)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

find_package(Git)
if(Git_FOUND)
    include(GetGitRevisionDescription)
    get_git_head_revision(GIT_REFSPEC GIT_SHA1)
    add_compile_definitions(GIT_SHA1="${GIT_SHA1}")
endif()

if (WIN32 OR APPLE)
    include(glfw)
else()  # Linux
    find_package(glfw3 REQUIRED)
    set(GLFW_LIBRARIES glfw)
endif()

find_package(OpenGL REQUIRED)
find_package(Threads REQUIRED)

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

include(CheckCXXCompilerFlag)

if (WIN32)
    add_compile_definitions(WIN32_LEAN_AND_MEAN)
    add_compile_definitions(NOMINMAX)
    add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
    add_compile_definitions(_SCL_SECURE_NO_WARNINGS)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /ENTRY:mainCRTStartup")
else()
    find_program(CCACHE_FOUND ccache)
    if (CCACHE_FOUND)
        set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
        set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
    endif (CCACHE_FOUND)
endif()

add_subdirectory(src)

SET(CPACK_PACKAGE_DIRECTORY ${CMAKE_BINARY_DIR}/packages)
SET(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
SET(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
SET(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
SET(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/LICENSE)

if (WIN32)
    configure_file(${CMAKE_SOURCE_DIR}/cmake/CPackWixPatch.cmake.in ${CMAKE_SOURCE_DIR}/cmake/wixpatch.xml @ONLY)
    configure_file("${CMAKE_SOURCE_DIR}/LICENSE" "${CMAKE_BINARY_DIR}/LICENSE.txt" COPYONLY)

    set(CPACK_GENERATOR WIX)
    set(CPACK_PACKAGE_INSTALL_DIRECTORY "ImStudio")
    set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_BINARY_DIR}/LICENSE.txt")
    set(CPACK_PACKAGE_EXECUTABLES "ImStudio" "ImStudio")
    set(CPACK_WIX_PATCH_FILE "${CMAKE_SOURCE_DIR}/cmake/wixpatch.xml")
    set(CPACK_WIX_PROPERTY_ARPURLINFOABOUT  "https://github.com/Raais/ImStudio")
    set(CPACK_WIX_PROGRAM_MENU_FOLDER "ImStudio")
    set(CPACK_WIX_UPGRADE_GUID "9BE85238-AE46-4597-AE56-9D719DDBF4B4")
elseif(APPLE)
    # TODO
    #set(MACOSX_BUNDLE_BUNDLE_NAME "ImStudio")
    #set(MACOSX_BUNDLE_GUI_IDENTIFIER "org.ImStudio")
    #set(MACOSX_BUNDLE_INFO_STRING "ImStudio")
    #set(MACOSX_BUNDLE_ICON_FILE "icns")
    #set(MACOSX_BUNDLE_LONG_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
    #set(MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
    #set(MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})

elseif(UNIX AND NOT APPLE)
    set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Raais <?? @ ?? .com>")
    set(CPACK_DEBIAN_FILE_NAME "ImStudio-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}-Ubuntu.deb")
    set(CPACK_DEBIAN_PACKAGE_DEPENDS "libglfw3")
    set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE all)
    set(CPACK_GENERATOR "TGZ;DEB")
endif()

include(CPack)
